<template>
  <a-card :title="detailForm.componentName">
    <template #extra>
      <a-popconfirm title="确认删除?" ok-text="是" cancel-text="否" @confirm="emit('delete')">
        <a href="#">删除</a>
      </a-popconfirm>
    </template>
    <a-form :model="detailFormState" :rules="formRules" ref="form" class="detail-form">
      <a-form-item name="componentCode" label="组件ID" :rules="[{ required: true, message: '组件ID' }]">
        <a-input v-model:value="detailFormState.componentCode" :disabled="true" />
      </a-form-item>

      <a-form-item name="type" label="布局容器">
        <a-select v-model:value="detailFormState.type" @change="change">
          <a-select-option v-for="item in options" :value="item.key" :key="item.key">
            {{ item.title }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="比例-宽" name="ratioW">
        <a-input v-model:value="detailFormState.ratioW" />
      </a-form-item>
      <a-form-item label="比例-高" name="ratioH">
        <a-input v-model:value="detailFormState.ratioH" />
      </a-form-item>
    </a-form>
  </a-card>
</template>

<script>
export default {
  name: 'DetailForm'
}
</script>

<script setup>
/**
 * 页面管理 - 页面配置 - 绑定详情组件表单
 * @author 阳坤
 * @date   时间：2023/2/1
 */

import { ref, defineProps, defineEmits, reactive, watch, defineExpose } from 'vue'
import { useStore } from 'vuex'

const emit = defineEmits(['delete'])
const props = defineProps({
  detailForm: {
    type: Object,
    default: () => {
      return {}
    }
  }
})

const store = useStore()

// 布局容器下拉列表
const options = store.state['page-manage-config'].containerList.filter(item => item.type == 'detail')
// 表单对象
const detailFormState = reactive({
  componentCode: '',
  type: '',
  ratioW: '',
  ratioH: ''
})
const formRules = {
  type: [{ required: true, message: '请选择对应的布局容器', trigger: 'blur' }],
  ratioW: [{ required: true, message: '请输入比例-宽', trigger: 'blur' }],
  ratioH: [{ required: true, message: '请输入比例-高', trigger: 'blur' }]
}

watch(
  () => props.detailForm,
  newValue => {
    detailFormState.componentCode = newValue.componentCode
    detailFormState.type = newValue.type
    detailFormState.ratioW = newValue.ratioW
    detailFormState.ratioH = newValue.ratioH
  },
  { deep: true, immediate: true }
)

/**
 * 选择 布局容器时
 * @param {*} e
 */
function change(e) {
  const data = options.filter(i => i.key === e)
  if (data.length) {
    detailFormState.ratioW = data[0].ratioW
    detailFormState.ratioH = data[0].ratioH
  }
}
defineExpose({ detailFormState })
</script>

<style scoped>
.detail-form {
}
</style>
